/**
 * 跳转之前先做鉴权
 * @param url (string)路由url
 */
export const toPageAuth = (url,paramsfun) => {
	const accessToken = uni.getStorageSync('accessToken')
	if (!accessToken) {
		uni.showModal({
			title: '您尚未登录',
			content: '是否去登录',
			confirmText: '去登录',
			success: res => {
				if (res.confirm) {
					uni.navigateTo({
						url: `/pages/login/index`
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	} else {
		uni.navigateTo({
			url: url,
			events:{},
			success:function(res){
				if(paramsfun){
					paramsfun(res)
				}
			}
		});
	}
}

export const requestUrl = ({
    url,
	data,
	success,
	method = 'post',
	header,
	loading = {}
})=>{
    if (loading.showload && !_loading) {
		uni.showLoading({
			mask: loading.mask || false,
			title: loading.showloadMsg || '加载中',
		});
		_loading = true
	}
    const token = uni.getStorageSync('accessToken');
    const _header=Object.assign({
        'content-type': 'application/json',
		'token': `${token}`,
    },header)
    return new Promise(function (resolve, reject) {
        uni.request({
            url: url,
			method: method,
			data: data,
            header:_header,
            success: (res) => {
                const resultData=res.data
                if (resultData.code == 401) {
                    uni.removeStorageSync('accessToken');
					uni.removeStorageSync('userInfo');
					uni.showToast({
						title: resultData.msg || '请求出错',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					uni.redirectTo({
						url: `/pages/login/index`
					})
					return
                }else if (resultData.code !== 200) {
                    uni.showToast({
						title: resultData.msg || '请求出错',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					reject(resultData);
                }else{
					resolve(resultData)
                }
            }
        })
    })
}


/**
 * @function 通过图片路径url获取图片的name和格式
 * @param {*} url 
 * @return {*}
 */
export const matchFilePath=(url)=>{
	if(url){
		const match = url.match(/\/([^/]+\.\w+)$/);
		const filename = match[1];

		const fileFomat=filename.match(/^([^/]+)\.(\w+)$/);

		return{
			url:url,
			name:fileFomat[1],
			extname:fileFomat[2]
		}
		
	}else{
		return null
	}
}


export const myrequestUrl = ({
    url,
	data,
	success,
	method = 'post',
	header,
	loading = {}
})=>{
    if (loading.showload && !_loading) {
		uni.showLoading({
			mask: loading.mask || false,
			title: loading.showloadMsg || '加载中',
		});
		_loading = true
	}
    const token = uni.getStorageSync('accessToken');
    const _header=Object.assign({
        'content-type': 'application/json',
		'token': `${token}`,
    },header)
    return new Promise(function (resolve, reject) {
        uni.request({
            url: url,
			method: method,
			data: data,
            header:_header,
            success: (res) => {
                const resultData=res.data
                if (resultData.code == 401) {
                    uni.removeStorageSync('accessToken');
					uni.removeStorageSync('userInfo');
					uni.showToast({
						title: resultData.msg || '请求出错',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					uni.redirectTo({
						url: `/pages/login/index`
					})
					return
                }else if (resultData.code !== 0) {
                    uni.showToast({
						title: resultData.msg || '请求出错',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					reject(resultData);
                }else{
					resolve(resultData)
                }
            }
        })
    })
}